package com.wdd.recoveryvehicle.vehicleInfo.controller;

import com.wdd.common.Result.Result;
import com.wdd.common.Result.ResultUtil;
import com.wdd.common.json.JsonUtil;
import com.wdd.common.utils.SnowFlake;
import com.wdd.recoveryvehicle.vehicleInfo.bean.VehicleInfoVB;
import com.wdd.recoveryvehicle.vehicleInfo.service.VehicleInfoService;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.hibernate.validator.constraints.NotBlank;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 车辆信息
 *
 * @author globalMr
 */
@Validated
@RequestMapping("/vehicleInfo")
@RestController
public class VehicleInfoController {

    private Logger logger = LoggerFactory.getLogger(VehicleInfoController.class);

    private SnowFlake snowFlake = new SnowFlake(2, 2);

    @Autowired
    private VehicleInfoService vehicleInfoService;

    /**
     * 插入 车辆信息
     *
     * @param vehicleInfoVB
     * @return
     */
    @PostMapping("/insertVehicleInfo")
    @RequiresPermissions({"insert"})
    public Result insertVehicleInfo(@Param("vehicleInfoVB") String vehicleInfoVB,
                                    @NotBlank(message = "user_id为空") @Param("user_id") String user_id,
                                    @NotBlank(message = "company_id为空") @Param("company_id") String company_id) {
        logger.info("insertVehicleInfo === 参数 " + vehicleInfoVB);

        VehicleInfoVB vehicleInfo = JsonUtil.toObject(vehicleInfoVB, VehicleInfoVB.class);

        // 生成车辆ID
        String id = "veh" + snowFlake.nextId();
        vehicleInfo.setVehicle_id(id);
        boolean flag1 = vehicleInfoService.insertVehicleInfo(vehicleInfo, user_id, company_id);
        boolean flah2 = vehicleInfoService.insertVehicleEmpRelation(vehicleInfo.getEmp_id(), vehicleInfo.getVehicle_id());
        if (flag1 && flah2) {
            return ResultUtil.success();
        } else {
            return ResultUtil.error(ResultUtil.ERROR_CODE, "插入车辆信息失败");
        }
    }

    /**
     * 查询 车辆信息
     *
     * @param useful
     * @return
     */
    @PostMapping("/selectVehicleInfoVBByUseful")
    public Result selectVehicleInfoVBByUseful(@NotBlank(message = "useful为空") String useful,
                                              @NotBlank(message = "company_id为空") @Param("company_id") String company_id) {
        logger.info("selectVehicleInfoVBByUseful === 参数 " + useful);
        List<VehicleInfoVB> vehicleInfoVBS = vehicleInfoService.selectVehicleInfoVBByUseful(useful, company_id);
        return ResultUtil.success(vehicleInfoVBS);
    }

    /**
     * 查询 车辆信息 --- 客户人员信息
     *
     * @param emp_id
     * @return
     */
    @PostMapping("/selectVehicleInfoVBByEmpId")
    public Result selectVehicleInfoVBByEmpId(@NotBlank(message = "emp_id为空") String emp_id) {
        logger.info("selectVehicleInfoVBByEmpId === 参数 " + emp_id);
        List<VehicleInfoVB> vehicleInfoVBS = vehicleInfoService.selectVehicleInfoVBByEmpId(emp_id);
        return ResultUtil.success(vehicleInfoVBS);
    }

    /**
     * 更新 车辆信息
     *
     * @param vehicleInfoVB
     * @return
     */
    @PostMapping("/updateVehicleInfo")
    @RequiresPermissions({"update"})
    public Result updateVehicleInfo(@Param("vehicleInfoVB") String vehicleInfoVB,
                                    @NotBlank(message = "user_id为空")@Param("user_id") String user_id) {
        logger.info("updateVehicleInfo === 参数 " + vehicleInfoVB);

        VehicleInfoVB vehicleInfo = JsonUtil.toObject(vehicleInfoVB, VehicleInfoVB.class);


        boolean flag1 = vehicleInfoService.updateVehicleInfo(vehicleInfo, user_id); // 更新 车辆
        boolean flag2 = vehicleInfoService.updateEmpVehicleRelation(vehicleInfo.getVehicle_id(), vehicleInfo.getEmp_id()); // 更新 车与人的关系
        if (flag1 && flag2) {
            return ResultUtil.success();
        } else {
            return ResultUtil.error(ResultUtil.ERROR_CODE, "更新车辆信息失败");
        }
    }

    /**
     * 更新 车辆信息无效
     *
     * @param vehicle_id
     * @return
     */
    @PostMapping("/updateVehicleInfoUsefulWx")
    @RequiresPermissions({"update"})
    public Result updateVehicleInfoUsefulWx(@NotBlank(message = "vehicle_id为空")@Param("vehicle_id")String vehicle_id,
                                            @NotBlank(message = "user_id为空")@Param("user_id")String user_id) {
        logger.info("updateVehicleInfoUsefulWx === 参数 " + vehicle_id);
        boolean flag = vehicleInfoService.updateVehicleInfoUsefulWx(vehicle_id, user_id);
        if (flag) {
            return ResultUtil.success();
        } else {
            return ResultUtil.error(ResultUtil.ERROR_CODE, "更新车辆信息失败");
        }
    }

}
